import toolHttp from "@/tool/http/ToolHttp.js";
const URL_PREFIX = "https://";
const cache = {};

const toolOpenApi = {
  aliyun: {
    geoJson: {
      baseUrl: `${URL_PREFIX}geo.datav.aliyun.com/areas_v3/bound/`,
      /**
       * 获取阿里云 geoJsonUrl - 不包含子区域
       * @param {Object} adCode
       */
      url(adCode = "100000") {
        return `${toolOpenApi.aliyun.geoJson.baseUrl}${adCode}.json`;
      },
      /**
       * 获取阿里云 geoJsonUrl - 包含子区域
       * @param {Object} adCode
       */
      urlFull(adCode = "100000") {
        return `${toolOpenApi.aliyun.geoJson.baseUrl}${adCode}_full.json`;
      },
      async geoJson(adCode = "100000") {
        let cacheCode = "geoJson" + adCode;
        if (!cache[cacheCode]) {
          cache[cacheCode] = await toolHttp.get(
            toolOpenApi.aliyun.geoJson.url(adCode)
          );
        }
        return cache[cacheCode];
      },
      async geoJsonFull(adCode = "100000") {
        let cacheCode = "geoJsonFull" + adCode;
        if (!cache[cacheCode]) {
          cache[cacheCode] = await toolHttp.get(
            toolOpenApi.aliyun.geoJson.urlFull(adCode)
          );
        }
        return cache[cacheCode];
      },
    },
  },
};

export default {};
